﻿using JianZhangTool.statistics.excel;
using JianZhangTool.tool;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace JianZhangTool.statistics
{
    public partial class GuardStatisticsForm : Form
    {
        public GuardStatisticsForm()
        {
            Instance = this;

            InitializeComponent();
            all_guard_listview.SelectedIndexChanged += all_guard_listview_SelectedIndexChanged;

            InitTable();
        }

        public static GuardStatisticsForm Instance;

        /// <summary>
        /// UID
        /// </summary>
        private Label text_uid;
        /// <summary>
        /// 用户名
        /// </summary>
        private Label text_uname;
        /// <summary>
        /// 曾用名查看按钮
        /// </summary>
        private Button button_oldnames;
        /// <summary>
        /// 舰长数文本
        /// </summary>
        private Label text_jzc;
        /// <summary>
        /// 提督数文本
        /// </summary>
        private Label text_tdc;
        /// <summary>
        /// 总督数文本
        /// </summary>
        private Label text_zdc;
        /// <summary>
        /// 深渊次数文本
        /// </summary>
        private Label text_syc;
        /// <summary>
        /// 深渊增加次数按钮
        /// </summary>
        private Button button_sy_inc;
        /// 深渊减少次数按钮
        /// </summary>
        private Button button_sy_dec;

        /// <summary>
        /// 当前的舰长信息
        /// </summary>
        private GuardInfo current_info;

        /// <summary>
        /// 所有的物品
        /// </summary>
        private List<ListViewItem> all_items = new List<ListViewItem>();

        /// <summary>
        /// 舰长信息改变
        /// </summary>
        /// <param name="guardInfo"></param>
        public void OnGuardInfoChange(GuardInfo guardInfo)
        {
            // 当前显示为改变的舰长的话 刷新信息
            if (guardInfo == current_info)
            {
                FillCurrentInfo();
            }
        }

        /// <summary>
        /// 销毁
        /// </summary>
        public void Destroy()
        {
            if (components != null)
            {
                components.Dispose();
            }
            base.Dispose(true);
        }

        /// <summary>
        /// 编辑表格内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InitTable()
        {
            Label title_uid = new Label();
            title_uid.Text = "UID";
            title_uid.Dock = DockStyle.Fill;
            title_uid.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            guardinfo_show_table.Controls.Add(title_uid, 0, 0);

            Label title_uname = new Label();
            title_uname.Text = "用户名";
            title_uname.Dock = DockStyle.Fill;
            title_uname.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            guardinfo_show_table.Controls.Add(title_uname, 0, 1);

            Label title_oldnames = new Label();
            title_oldnames.Text = "曾用名";
            title_oldnames.Dock = DockStyle.Fill;
            title_oldnames.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            guardinfo_show_table.Controls.Add(title_oldnames, 0, 2);

            Label title_jzc = new Label();
            title_jzc.Text = "舰长次数";
            title_jzc.Dock = DockStyle.Fill;
            title_jzc.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            guardinfo_show_table.Controls.Add(title_jzc, 0, 3);

            Label title_tdc = new Label();
            title_tdc.Text = "提督次数";
            title_tdc.Dock = DockStyle.Fill;
            title_tdc.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            guardinfo_show_table.Controls.Add(title_tdc, 0, 4);

            Label title_zdc = new Label();
            title_zdc.Text = "总督次数";
            title_zdc.Dock = DockStyle.Fill;
            title_zdc.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            guardinfo_show_table.Controls.Add(title_zdc, 0, 5);

            Label title_syc = new Label();
            title_syc.Text = "深渊次数";
            title_syc.Dock = DockStyle.Fill;
            title_syc.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            guardinfo_show_table.Controls.Add(title_syc, 0, 6);

            text_uid = new Label();
            text_uid.Dock = DockStyle.Left;
            text_uid.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            guardinfo_show_table.Controls.Add(text_uid, 1, 0);

            text_uname = new Label();
            text_uname.Dock = DockStyle.Left;
            text_uname.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            guardinfo_show_table.Controls.Add(text_uname, 1, 1);

            button_oldnames = new Button();
            button_oldnames.Text = "查看";
            button_oldnames.Click += button_oldnames_Click;
            guardinfo_show_table.Controls.Add(button_oldnames, 1, 2);
            button_oldnames.Enabled = false;

            text_jzc = new Label();
            text_jzc.Dock = DockStyle.Left;
            text_jzc.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            guardinfo_show_table.Controls.Add(text_jzc, 1, 3);

            text_tdc = new Label();
            text_tdc.Dock = DockStyle.Left;
            text_tdc.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            guardinfo_show_table.Controls.Add(text_tdc, 1, 4);

            text_zdc = new Label();
            text_zdc.Dock = DockStyle.Left;
            text_zdc.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            guardinfo_show_table.Controls.Add(text_zdc, 1, 5);

            Panel panel = new Panel();
            panel.Dock = DockStyle.Fill;
            text_syc = new Label();
            text_syc.Dock = DockStyle.Left;
            text_syc.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            text_syc.AutoSize = false;
            text_syc.Size = new System.Drawing.Size(29, 100);

            button_sy_inc = new Button();
            button_sy_inc.Left = 105;
            button_sy_inc.Width = 29;
            button_sy_inc.Text = "+";
            button_sy_inc.Click += button_sy_inc_Click;

            button_sy_dec = new Button();
            button_sy_dec.Left = 145;
            button_sy_dec.Width = 29;
            button_sy_dec.Text = "-";
            button_sy_dec.Click += button_sy_dec_Click;

            panel.Controls.Add(text_syc);
            panel.Controls.Add(button_sy_inc);
            panel.Controls.Add(button_sy_dec);
            button_sy_dec.Enabled = false;
            guardinfo_show_table.Controls.Add(panel, 1, 6);
        }

        /// <summary>
        /// 查看曾用名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_oldnames_Click(object sender, EventArgs e)
        {
            if (current_info == null)
            {
                return;
            }
            if (current_info.old_unames.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (string oldname in current_info.old_unames)
                {
                    if (sb.Length != 0)
                    {
                        sb.Append(Environment.NewLine);
                    }
                    sb.Append(oldname);
                }
                Tools.alert_message("曾用名", sb.ToString());
            }
        }

        /// <summary>
        /// 增加深渊次数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_sy_inc_Click(object sender, EventArgs e)
        {
            if (current_info != null)
            {
                current_info.IncShenyuanNum();
                FillCurrentInfo();
            }
        }

        /// <summary>
        /// 扣除深渊次数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_sy_dec_Click(object sender, EventArgs e)
        {
            if (current_info != null)
            {
                current_info.DecShenyuanNum();
                FillCurrentInfo();
            }
        }

        /// <summary>
        /// 点选改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void all_guard_listview_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (all_guard_listview.SelectedItems != null && all_guard_listview.SelectedItems.Count > 0)
            {
                SwitchToGuard((int)all_guard_listview.SelectedItems[0].Tag);
            }
        }

        /// <summary>
        /// 重新加载房间信息
        /// </summary>
        public void ReloadCurrentRoomInfo()
        {
            RoomGuardInfos guardInfos = Main.Instance.GetRoomGuardInfos();
            if (guardInfos == null)
            {
                return;
            }
            List<GuardInfo> infolist = guardInfos.GetInfoList();
            if (infolist.Count == 0)
            {
                return;
            }
            all_items.Clear();
            all_guard_listview.Items.Clear();
            search_textbox.Text = "";
            foreach (GuardInfo guardInfo in infolist)
            {
                ListViewItem item = new ListViewItem();
                item.Text = guardInfo.uname;
                item.Tag = guardInfo.uid;
                all_items.Add(item);
                all_guard_listview.Items.Add(item);
            }
            all_guard_listview.Items[0].Selected = true;
            all_guard_listview.Select();
        }

        /// <summary>
        /// 切换到UID对应的信息
        /// </summary>
        /// <param name="uid"></param>
        public void SwitchToGuard(int uid)
        {
            RoomGuardInfos guardInfos = Main.Instance.GetRoomGuardInfos();
            if (guardInfos == null)
            {
                return;
            }
            current_info = guardInfos.GetGuardInfoByUID(uid);
            if (current_info == null)
            {
                return;
            }
            // 填充Table信息
            FillCurrentInfo();
        }

        /// <summary>
        /// 填充Table信息
        /// </summary>
        private void FillCurrentInfo()
        {
            if (current_info == null)
            {
                return;
            }
            text_uid.Text = current_info.uid + "";
            text_uname.Text = current_info.uname;
            button_oldnames.Enabled = current_info.old_unames.Count > 0;
            text_jzc.Text = current_info.jianzhang_num + "";
            text_tdc.Text = current_info.tidu_num + "";
            text_zdc.Text = current_info.zongdu_num + "";
            text_syc.Text = current_info.shenyuan_num + "";
            button_sy_dec.Enabled = current_info.shenyuan_num > 0;
        }

        /// <summary>
        /// 用户名检索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void search_button_Click(object sender, EventArgs e)
        {
            string part = search_textbox.Text;
            if (part == null || part.Equals(""))
            {
                all_guard_listview.Items.Clear();
                all_guard_listview.Items.AddRange(all_items.ToArray());
                all_guard_listview.Items[0].Selected = true;
                all_guard_listview.Select();
            }
            else
            {
                List<ListViewItem> tempList = new List<ListViewItem>();
                foreach (ListViewItem item in all_items)
                {
                    GuardInfo guardInfo = Main.Instance.GetRoomGuardInfos().GetGuardInfoByUID((int)item.Tag);
                    if (guardInfo == null)
                    {
                        continue;
                    }
                    if (guardInfo.uname.Contains(part))
                    {
                        tempList.Add(item);
                        continue;
                    }
                    foreach (string oldname in guardInfo.old_unames)
                    {
                        if (oldname.Contains(part))
                        {
                            tempList.Add(item);
                            continue;
                        }
                    }
                }
                if (tempList.Count == 0)
                {
                    Tools.alert_message("错误", "未搜索到相关信息!");
                }
                else
                {
                    all_guard_listview.Items.Clear();
                    all_guard_listview.Items.AddRange(tempList.ToArray());
                    all_guard_listview.Items[0].Selected = true;
                    all_guard_listview.Select();
                }
            }
        }

        /// <summary>
        /// 清除检索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sclear_button_Click(object sender, EventArgs e)
        {
            all_guard_listview.Items.Clear();
            all_guard_listview.Items.AddRange(all_items.ToArray());
            all_guard_listview.Items[0].Selected = true;
            all_guard_listview.Select();
            // 同时清除输入框的内容
            search_textbox.Text = "";
        }

        /// <summary>
        /// Excel文件导入数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void st_input_button_Click(object sender, EventArgs e)
        {
            if (Main.Instance.GetRoomGuardInfos() == null)
            {
                return;
            }
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = false;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                // 获取文件的完整路径
                string filename = fileDialog.FileName;
                if (MessageBox.Show("导入文件将覆盖当前记录!", "注意", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    List<GuardInfo> infos = ExcelInput.ParseFromFile(filename);
                    if (infos == null)
                    {
                        Tools.alert_message("错误", "导入文件失败!");
                        return;
                    }
                    else
                    {
                        Main.Instance.GetRoomGuardInfos().ReloadFromRecords(infos);
                        ReloadCurrentRoomInfo();
                        Tools.alert_message("成功", "导入文件成功!");
                    }
                }
            }
        }

        /// <summary>
        /// 导出Excel文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void st_output_button_Click(object sender, EventArgs e)
        {
            if (Main.Instance.GetRoomGuardInfos() == null)
            {
                return;
            }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            // 设置默认文件名
            fileDialog.FileName = "航海统计汇总";
            // 主设置默认文件扩展名
            fileDialog.DefaultExt = "xlsx";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string filename = fileDialog.FileName;
                if (!filename.EndsWith(".xlsx"))
                {
                    filename += ".xlsx";
                }
                if (!ExcelOutput.SaveIntoFile(filename))
                {
                    Tools.alert_message("错误", "导出文件失败!");
                }
                else
                {
                    Tools.alert_message("成功", "导出文件成功!");
                }
            }

        }
    }
}
